//index.js
//获取应用实例
const app = getApp()
var page = 1;
var post_list = [];
let loadingMore = false;
let loadedEnd = false;
Page({
  data: {
    userInfo: {},
    user_id: '',
    post: [],
    adv:null
  },
  onReady: function () {
    this.post = this.selectComponent("#post");
  },
  //事件处理函数
  onLoad: function () {
    wx.startPullDownRefresh();
    var value = wx.getStorageSync('user_id');
    if (value) {
      this.getPostList(page);
    } else {
      app.login()
        .then(app.doLogin)
        .then(app.checkAuth('scope.userInfo'))
        .then(app.getUserInfo)
        .then(app.setUserData)
        .then(this.getPostList(page))
        .catch(app.toLoad)
    }
  },
  getPostList: function (page) {
    let that = this;
    wx.showToast({
      title: "数据加载中",
      icon: "loading"
    });
    return new Promise((resolve, reject)=>{
      wx.request({
        url: 'https://rtutrip.aihuodong.top/index/index/get_post_list',
        data: {
          plate_id: 0,
          page:page
        },
        success(res) {
          console.log(res.data.data);
          if (res.data.data.post.length == 0) {
            loadedEnd = true;
            wx.showToast({
              title: "已经到底啦",
              icon: "success"
            });
          }
          for (var i = 0; i < res.data.data.post.length; i++) {
            post_list.push(res.data.data.post[i]);
          }
          that.setData({
            post: post_list,
            adv:res.data.data.ad
          })
          resolve(res);
          wx.stopPullDownRefresh();
        }
      })
    })
  },
  onReachBottom() {
    if (loadingMore || loadedEnd) return false;
    loadingMore = true;
    wx.showToast({
      title: "数据加载中",
      icon: "loading"
    });
    page += 1;
    this.getPostList(page);
    loadingMore = false;
  },
  onShareAppMessage: function (e) {
    return {
      title: '塞浦路斯土耳其免费信息平台',
      path: '/pages/index/index'
    }
  }
})